<template>
  <div class="layout">
    <div class="bottom" :class="animBounceInUp">
      <el-card class="aside">
        <div>类区域</div>
        <div v-for="i in classList">
<!--          <el-tooltip class="box-item" effect="dark" :content="i.comment" placement="right">-->
            <el-button @click="onSelClass(i.cmd)" :style="{'background-color': i===selClass ? '#DCE2F1':''}">
              {{i.cmd}}-{{ i.actionSimpleName }}
            </el-button>
<!--          </el-tooltip>-->
        </div>
      </el-card>
      <el-card class="aside">
        <div>方法区域</div>
        <div class="aside-method">
          <div v-for="i in methodList">
<!--            <el-tooltip class="box-item" effect="dark" :content="i.comment" placement="right">-->
              <el-button @click="onSelMethod(i)"
                         :style="{'background-color':  i.methodName===selMethod ? '#DCE2F1':''}">
                {{selClass}}-{{i.subCmd}}-{{ i.methodName }}
              </el-button>
<!--            </el-tooltip>-->
          </div>
        </div>
      </el-card>
      <el-card class="aside-input">
        <div>参数区域</div>
        <el-button v-if="selMethod!==''" style="width: 100%;margin-bottom: 16px" type="primary" @click="onSend">发送
        </el-button>
        <div class="aside-param">
          <el-form label-position="left" label-width="150px">
            <el-form-item :label="key" v-for="(val, key, index) in paramMap">
              <el-tooltip class="box-item" effect="dark" :content="methodParams[index].type" placement="right">
                <el-input v-model="paramMap[key]"></el-input>
              </el-tooltip>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <el-card class="main">
        <div class="json-data">
          <pre>{{ result }}</pre>
        </div>
      </el-card>
    </div>
  </div>
</template>


<script setup>
import {animBounceInUp} from "../assets/anim";
import {ref} from "vue";
import axios from "axios";
import {initWebSocket} from "../util/webSocket";
import {ElNotification} from "element-plus";

// ws链接
let classList = ref([])//类列表
let methodList = ref([])//方法列表
let paramMap = ref({})//参数map
let selClass = ref(0)//选择类
let selMethod = ref(null)//选择方法
let result = ref("") //结果
let methodParams = ref("")//方法参数

// 获取选择的cls的所有方法
function onSelClass(v) {
  selClass.value = v;
  selMethod.value = ""
  paramMap.value = {}
  axios.get("/cmd/" + v, {
    params: {
      key: v
    }
  }).then((response) => {
    methodList.value = response.data
    // onCssAnim(".aside-method", bounceInUpList, () => {
    // })
  })
}

// 获取方法参数
function onSelMethod(v) {
  selMethod.value = v

  paramMap.value = {}
  methodParams.value = v.actionParamFieldDtoList
  for (let k in v.actionParamFieldDtoList) {
    paramMap.value[v.actionParamFieldDtoList[k].name] = ""
  }

  // onCssAnim(".aside-param", bounceInUpList, () => {
  // })
}

axios.get("/cmd").then((response) => {
  classList.value = response.data
})

let webSocket = initWebSocket("ws://localhost:18080/client");
webSocket.onmessage = (e) => {
  // 需要转换一下
  let parse = JSON.parse(e.data);
  result.value = JSON.parse(parse.data)
}

function onSend() {
  let message = {}
  message.data = paramMap.value
  message.cmdMerge = selMethod.value.cmdMerge
  message.responseStatus = 0
  console.log(message)
  webSocket.send(JSON.stringify(message))
}


ElNotification({
  title: '新页面已推出！',
  dangerouslyUseHTMLString: true,
  message: ' <a href="#/plus">访问<i>新页面</i></a> ',
})

</script>

<style scoped lang="scss">
* {
  background-color: #FFFFFF;
}

.layout {


  .el-card {
    margin: 8px;
  }

  height: 100%;
  display: flex;
  flex-direction: column;

  .header {
    display: flex;
    align-items: center;

    * {
      margin-left: 30px;
    }

    .el-input {
      width: 300px;
    }
  }

  .bottom {
    height: 100%;
    display: flex;


    .aside, .aside-input {

      .el-button {
        margin: 4px;
        width: 100%;
      }

      width: 250px;
      text-align: center;
    }

    .aside-input {
      width: 350px;
    }

    .main {
      font-size: 18px;
      flex: 1;
    }

  }


}
</style>